#include"RingQueue.hpp"
#include"Thread.hpp"
#include"Task.hpp"
#include<string>
#include<vector>
#include<unistd.h>
#include<ctime>
    //向队列中投递任务
    using namespace ThreadModule;
    using ringqueue_t = RingQueue<Task>;
    void Consumer(ringqueue_t &rq,std::string name)
    {

        while (true )
        {
         sleep(2);
         //消费
         Task t;
         rq.Pop(&t);
         //消费头个任务
         std::cout<<"Consumer headler task:"<<"["<<name<<"]"<<std::endl;
            //处理
         t();
        }
    }
    void Productor(ringqueue_t &rq,std::string name)
    {
        srand(time(nullptr)^pthread_self());

        while (true)
        {
            //获取任务
            //生产任务
            rq.Enqueue(Downlioad);
            std::cout <<"productor :"<<"["<<name<<"]"<<std::endl;
          
        }
        
    }
    void InitComm (std::vector<Thread<ringqueue_t>> *thread,int num,ringqueue_t &rq,func_t<ringqueue_t> func,const std::string &who)
    {
            for(int i = 0;i<num;++i)
            {
                std::string name = "thread-"+std::to_string(i+1)+"-"+who;
                thread->emplace_back(func,rq,name);
            }
    }
    void Initproductor(std::vector<Thread<ringqueue_t>> *threads,int mun,ringqueue_t &rq)
    {
        InitComm(threads,mun,rq,Consumer,"consumer");
    }
    void InitConsumer(std::vector<Thread<ringqueue_t>> *threads,int mun,ringqueue_t &rq)
    {
        InitComm(threads,mun,rq,Productor,"productor");
    }

    void StartAll(std::vector<Thread<ringqueue_t>> &threads)
    {
        for(auto &thread:threads)
        {
            std::cout<<"start:"<<thread.name()<<std::endl;
            thread.Start();
        }
    }
    void WaitALLThread(std::vector<Thread<ringqueue_t>> &threads)
    {
        for(auto &thread:threads)
        {
            thread.Join();
        }
    }
int main()
{
    ringqueue_t *rq = new ringqueue_t(10);
    std::vector<Thread<ringqueue_t>> threads;
    Initproductor(&threads,1,*rq);//生产
    InitConsumer(&threads,1,*rq);

    StartAll(threads);
    WaitALLThread(threads);
    return 0;
}